import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
public class StreamTest {
    public static void main(String[] args) {

        List<String> stringList = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");

        System.out.println("統計空字符串數量:");
        long strings = stringList.stream().filter(string -> string.isEmpty()).count();
        System.out.println(strings);

        System.out.println("循环遍历输出集合:");
        stringList.forEach(res -> System.out.print(res + " "));
        System.out.println();

        System.out.println("使用map映射每个元素对应的结果:");
        List<Integer> integerList = Arrays.asList(1, 2, 2, 3, 4, 5);
        List<Integer> integerList1 = integerList.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        integerList1.forEach(res -> System.out.print(res + " "));
        System.out.println();

        System.out.println("使用limit限制随机数量:");
        Random random1 = new Random();
        random1.ints().limit(5).forEach(System.out::println);

        System.out.println("sorted 方法用于对流进行排序:");
        Random random2 = new Random();
        random2.ints(10).limit(5).sorted().forEach(System.out::println);

        System.out.println("Collectors 类:");
        List<String> strings1 = stringList.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        strings1.forEach(res ->System.out.print(res+" "));
        System.out.println();

        System.out.println("合并字符串:");
        String strings2  = stringList.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining());
        System.out.println(strings2);

        System.out.println("统计：");
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        IntSummaryStatistics stas = numbers.stream().mapToInt(x->x).summaryStatistics();
        System.out.println("列表中最小的数："+ stas.getMin());
        System.out.println("列表中最大的数：" +stas.getMax());
        System.out.println("列表的平均数："+ stas.getAverage());

    }
}
